home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 559 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  46 lines

  1. Path: news.capitalnet.com!usenet
  2. From: wingl@capitalnet.com
  3. Newsgroups: comp.lang.c++
  4. Subject: Help: How to initialize objects created by new class[x]
  5. Date: 5 Jan 1996 05:19:34 GMT
  6. Organization: Capitalnet
  7. Message-ID: <4cicd6$lsv@news.capitalnet.com>
  8. Reply-To: wingl@bnr.ca
  9. NNTP-Posting-Host: user77.capitalnet.com
  10. Keywords: new array
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. Let say I have the following class
  14.  
  15. My_class {
  16.   public:
  17.     int (int i=0) : a(i), b(i) {}
  18.   private:
  19.     int a, b;
  20. }
  21.  
  22. For the following statement, default constructor is involved upon 
  23. the creation of the object.
  24.  
  25.            My_class* my_ptr= new My_class;
  26.  
  27. However,  if I use  
  28.                          My_class* my_ptr= new My_class[10];
  29.  
  30. to allocate 10 objects of class My_class, then C++ would not
  31. call any constructor to do any initialization.  All the objects
  32. allocated are not initializated at all. 
  33.  
  34. I wonder what is the proper way to perform object initialization
  35. whenever an array of objects are created?  As long as a constructor
  36. is involved for each of the object in the array, I don't care if all the 
  37. objects are initializated the same or not.
  38.  
  39. If there is no way to get constructor to initialize an array of objects
  40. upon creation.  Does that means one should not do so?
  41.  
  42. Thanks for any help.
  43.  
  44. Wing
  45. wingl@bnr.ca
  46.